# -*- coding:utf-8 -*-
"""
@Time : 2021/1/11 17:22
@Author: langengel
@Des: 服务商基本模型
"""
from tortoise import Model, fields


class User(Model):
    u_id = fields.CharField(null=True, max_length=11, unique=True, description="用户业务ID")
    username = fields.CharField(null=True, max_length=20, description="用户名")
    type = fields.BooleanField(default=False, description="用户类型 True:超级管理员 False:普通管理员")
    password = fields.CharField(null=True, max_length=255)
    nickname = fields.CharField(default='阑羹阁兒', max_length=255, description='昵称')
    u_phone = fields.CharField(null=True, description="手机号", max_length=11)
    u_email = fields.CharField(null=True, description='邮箱', max_length=255)
    full_name = fields.CharField(null=True, description='姓名', max_length=255)
    u_status = fields.IntField(default=0, description='0未激活 1正常 2禁用')
    headimgurl = fields.CharField(null=True, max_length=255, description='头像')
    sex = fields.IntField(default=0, null=True, description='0未知 1男 2女')
    remarks = fields.CharField(null=True, max_length=30, description="备注")
    client_host = fields.CharField(null=True, max_length=19, description="访问IP")
    create_time = fields.DatetimeField(auto_now_add=True, description='创建时间')
    update_time = fields.DatetimeField(auto_now=True, description="更新时间")
    roles = fields.ManyToManyField("default.Role", related_name="user_roles", on_delete=fields.CASCADE)
    service = fields.ForeignKeyField("default.Service", related_name="user_service", on_delete=fields.CASCADE)
    is_really = fields.BooleanField(default=False, description="实名状态")

    class Meta:
        table_description = "用户"
        table = "s_user"


class Service(Model):
    s_id = fields.CharField(null=True, max_length=11, unique=True, description="服务商ID")
    service_name = fields.CharField(max_length=30, description='服务商名称')
    service_tel = fields.CharField(max_length=12, default='0851-1111111', description='服务商座机')
    service_phone = fields.CharField(null=True, max_length=11, description='服务商紧急联系手机号')
    service_logo = fields.CharField(null=True, max_length=255, description='服务商logo')
    service_abstract = fields.TextField(default='我们是一家酷公司', description='服务商摘要')
    Business_scope = fields.TextField(default='我们做最好的服务！', description='经营范围')
    create_time = fields.DatetimeField(auto_now_add=True, description='注册时间')
    expire_time = fields.DatetimeField(auto_now=True, description='到期时间')
    update_time = fields.DatetimeField(auto_now=True, description="更新时间")

    class Meta:
        table_description = "服务商"
        table = "s_service"


class Role(Model):
    role_name = fields.CharField(max_length=15, description="角色名称")
    rules = fields.ManyToManyField("default.Rule", related_name="role_rule", on_delete=fields.CASCADE)
    service = fields.ForeignKeyField("default.Service", related_name="service_role")
    role_status = fields.BooleanField(default=True, description="True:启用 False:禁用")
    role_desc = fields.CharField(null=True, max_length=255, description='角色描述')
    create_time = fields.DatetimeField(auto_now_add=True, description='创建时间')
    update_time = fields.DatetimeField(auto_now=True, description="更新时间")

    class Meta:
        table_description = "服务商用户角色"
        table = "s_role"


class Rule(Model):
    rule_name = fields.CharField(max_length=15, description="权限名称")
    parent_id = fields.IntField(default=0, description='父id')
    scopes = fields.CharField(unique=True, max_length=255, description='权限范围标识')
    rule_desc = fields.CharField(null=True, max_length=255, description='权限描述')
    is_check = fields.IntField(default=1, description='权限状态 1权限 2必选权限')
    is_menu = fields.IntField(default=2, description='是否为菜单 1菜单 2不是菜单')
    create_time = fields.DatetimeField(auto_now_add=True, description="创建时间")
    update_time = fields.DatetimeField(auto_now=True, description="更新时间")

    class Meta:
        table_description = "系统权限"
        table = "s_rule"


class Wechat(Model):
    city = fields.CharField(null=True, max_length=255, description='城市')
    country = fields.CharField(null=True, max_length=255, description='国家')
    headimgurl = fields.CharField(null=True, max_length=255, description='微信头像')
    nickname = fields.CharField(null=True, max_length=255, description='微信昵称')
    openid = fields.CharField(null=True, max_length=255, description='openid')
    province = fields.CharField(null=True, max_length=255, description='省份')
    sex = fields.IntField(null=True, description='性别')
    create_time = fields.DatetimeField(auto_now_add=True, description="创建时间")
    update_time = fields.DatetimeField(auto_now=True, description="更新时间")
    service = fields.ForeignKeyField("default.Service", related_name="service_wechat", on_delete=fields.CASCADE)
    user = fields.ForeignKeyField("default.User", related_name="wechat_user", on_delete=fields.CASCADE)

    class Meta:
        table_description = "用户微信"
        table = "s_wechat"



class Location(Model):
    region_Id = fields.BigIntField(pk=True, description="地区Id")
    pid = fields.IntField(default=0, description="地区父节点")
    region_code = fields.CharField(max_length=10, description="地区编码")
    name = fields.CharField(max_length=50, description="地区名")
    level = fields.IntField(default=1, description="地区级别")

    class Meta:
        table_description = "地区编码"
        table = "s_location"
